package com.ruoyi.project.system.service.impl;

import java.util.List;

import com.ruoyi.project.cipher.SM3Digest;
import com.ruoyi.project.cipher.SM4Utils;
import org.bouncycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.mapper.DemoMapper;
import com.ruoyi.project.system.domain.Demo;
import com.ruoyi.project.system.service.IDemoService;

/**
 * demoService业务层处理
 * 
 * @author ruoyi
 * @date 2020-02-17
 */
@Service
public class DemoServiceImpl implements IDemoService 
{
    @Autowired
    private DemoMapper demoMapper;

    /**
     * 查询demo
     * 
     * @param id demoID
     * @return demo
     */
    @Override
    public Demo selectDemoById(Integer id)
    {
        Demo demo =demoMapper.selectDemoById(id);
        //SM4算法将ECB加密的内容解密
        SM4Utils sm4 = new SM4Utils();
        sm4.setSecretKey("JeF8U9wHFOMfs2Y8");
        sm4.setHexString(false);
        demo.setBankid(sm4.decryptData_ECB(demo.getBankid()));
        return demo;
    }

    /**
     * 查询demo列表
     * 
     * @param demo demo
     * @return demo
     */
    @Override
    public List<Demo> selectDemoList(Demo demo)
    {
        List<Demo> list=demoMapper.selectDemoList(demo);
        //SM4算法将ECB加密的内容解密
        SM4Utils sm4 = new SM4Utils();
        sm4.setSecretKey("JeF8U9wHFOMfs2Y8");
        sm4.setHexString(false);
        for (Demo s : list) {
            s.setBankid(sm4.decryptData_ECB(s.getBankid()));
        }
        return list;
    }

    /**
     * 新增demo
     * 
     * @param demo demo
     * @return 结果
     */
    @Override
    public int insertDemo(Demo demo)
    {
        //SM4算法通过ECB编码将内容加密
        SM4Utils sm4 = new SM4Utils();
        sm4.setSecretKey("JeF8U9wHFOMfs2Y8");
        sm4.setHexString(false);
        demo.setBankid(sm4.encryptData_ECB(demo.getBankid()));
        //SM3算法对密码进行加密
        byte[] md = new byte[32];
        byte[] msg1 = demo.getPassword().getBytes();
        SM3Digest sm3 = new SM3Digest();
        sm3.update(msg1, 0, msg1.length);
        sm3.doFinal(md, 0);
        String s = new String(Hex.encode(md));
        demo.setPassword(s);
        return demoMapper.insertDemo(demo);
    }

    /**
     * 修改demo
     * 
     * @param demo demo
     * @return 结果
     */
    @Override
    public int updateDemo(Demo demo)
    {
        //SM4算法通过ECB编码将内容加密
        SM4Utils sm4 = new SM4Utils();
        sm4.setSecretKey("JeF8U9wHFOMfs2Y8");
        sm4.setHexString(false);
        demo.setBankid(sm4.encryptData_ECB(demo.getBankid()));
        //SM3算法对密码进行加密
        byte[] md = new byte[32];
        byte[] msg1 = demo.getPassword().getBytes();
        SM3Digest sm3 = new SM3Digest();
        sm3.update(msg1, 0, msg1.length);
        sm3.doFinal(md, 0);
        String s = new String(Hex.encode(md));
        demo.setPassword(s);
        return demoMapper.updateDemo(demo);
    }

    /**
     * 批量删除demo
     * 
     * @param ids 需要删除的demoID
     * @return 结果
     */
    @Override
    public int deleteDemoByIds(Integer[] ids)
    {
        return demoMapper.deleteDemoByIds(ids);
    }

    /**
     * 删除demo信息
     * 
     * @param id demoID
     * @return 结果
     */
    @Override
    public int deleteDemoById(Integer id)
    {
        return demoMapper.deleteDemoById(id);
    }
}
